# -*- makefile -*-
#
# Makefile for building NASM using Microsoft Visual C++ and NMAKE.
# Tested on Microsoft Visual C++ 2005 Express Edition.
#
# Make sure to put the appropriate directories in your PATH, in
# the case of MSVC++ 2005, they are ...\VC\bin and ...\Common7\IDE.

top_srcdir	= .
srcdir		= .
VPATH		= .
prefix		= C:\Program Files\NASM
exec_prefix	= $(prefix)
bindir		= $(prefix)/bin
mandir		= $(prefix)/man

CC		= cl.exe
LINKEXE = link.exe
LIBEXE = lib.exe

CFLAGS_BASE = /c /MD
!if "$(BUILD)" == "release"
CFLAGS_BASE = $(CFLAGS_BASE) /O2
LFLAGS_BASE = $(LFLAGS_BASE) 
!else
CFLAGS_BASE = $(CFLAGS_BASE) /Zi
LFLAGS_BASE = $(LFLAGS_BASE) /DEBUG
!endif

INTERNAL_CFLAGS = /I$(srcdir)/inttypes /I$(srcdir) /I. /DHAVE__SNPRINTF /DHAVE__VSNPRINTF

ALL_CFLAGS	= $(CFLAGS_BASE) $(INTERNAL_CFLAGS)

LDFLAGS		= $(LFLAGS_BASE) /SUBSYSTEM:CONSOLE
LIBS		= 
PERL		= perl -I$(srcdir)/perllib

debug:
	$(MAKE) BUILD=debug    -f Mkfiles\vc.mak  all
release:
	$(MAKE) BUILD=release  -f Mkfiles\vc.mak  all


# Binary suffixes
O               = obj
X               = .exe

.SUFFIXES: .c .i .s .obj .1 .man

.c.obj:
	$(CC) $(ALL_CFLAGS) /Fo$@ $<

#-- Begin File Lists --#
# Edit in Makefile.in, not here!
NASM =	nasm.obj nasmlib.obj ver.obj \
	raa.obj saa.obj rbtree.obj \
	float.obj insnsa.obj insnsb.obj \
	directiv.obj \
	assemble.obj labels.obj hashtbl.obj crc64.obj parser.obj \
	output/outform.obj output/outlib.obj output/nulldbg.obj \
	output/nullout.obj \
	output/outbin.obj output/outaout.obj output/outcoff.obj \
	output/outelf.obj output/outelf32.obj output/outelf64.obj \
	output/outelfx32.obj \
	output/outobj.obj output/outas86.obj output/outrdf2.obj \
	output/outdbg.obj output/outieee.obj output/outmac32.obj \
	output/outmac64.obj preproc.obj quote.obj pptok.obj \
	macros.obj listing.obj eval.obj exprlib.obj stdscan.obj \
	strfunc.obj tokhash.obj regvals.obj regflags.obj \
	ilog2.obj \
	lib/strlcpy.obj \
	preproc-nop.obj

NDISASM = ndisasm.obj disasm.obj sync.obj nasmlib.obj ver.obj \
	insnsd.obj insnsb.obj insnsn.obj regs.obj regdis.obj
#-- End File Lists --#

all: nasm.exe ndisasm.exe
	rem cd rdoff && $(MAKE) all

nasm.exe: $(NASM)
	$(LINKEXE) $(LDFLAGS) /OUT:nasm.exe $(NASM) $(LIBS)

ndisasm.exe: $(NDISASM)
	$(LINKEXE) $(LDFLAGS) /OUT:ndisasm.exe $(NDISASM) $(LIBS)

# These source files are automagically generated from a single
# instruction-table file by a Perl script. They're distributed,
# though, so it isn't necessary to have Perl just to recompile NASM
# from the distribution.

insnsb.c: insns.dat insns.pl
	$(PERL) $(srcdir)/insns.pl -b $(srcdir)/insns.dat
insnsa.c: insns.dat insns.pl
	$(PERL) $(srcdir)/insns.pl -a $(srcdir)/insns.dat
insnsd.c: insns.dat insns.pl
	$(PERL) $(srcdir)/insns.pl -d $(srcdir)/insns.dat
insnsi.h: insns.dat insns.pl
	$(PERL) $(srcdir)/insns.pl -i $(srcdir)/insns.dat
insnsn.c: insns.dat insns.pl
	$(PERL) $(srcdir)/insns.pl -n $(srcdir)/insns.dat

# These files contains all the standard macros that are derived from
# the version number.
version.h: version version.pl
	$(PERL) $(srcdir)/version.pl h < $(srcdir)/version > version.h

version.mac: version version.pl
	$(PERL) $(srcdir)/version.pl mac < $(srcdir)/version > version.mac

# This source file is generated from the standard macros file
# `standard.mac' by another Perl script. Again, it's part of the
# standard distribution.

macros.c: macros.pl standard.mac version.mac
	$(PERL) $(srcdir)/macros.pl $(srcdir)/standard.mac version.mac

# These source files are generated from regs.dat by yet another
# perl script.
regs.c: regs.dat regs.pl
	$(PERL) $(srcdir)/regs.pl c $(srcdir)/regs.dat > regs.c
regflags.c: regs.dat regs.pl
	$(PERL) $(srcdir)/regs.pl fc $(srcdir)/regs.dat > regflags.c
regdis.c: regs.dat regs.pl
	$(PERL) $(srcdir)/regs.pl dc $(srcdir)/regs.dat > regdis.c
regvals.c: regs.dat regs.pl
	$(PERL) $(srcdir)/regs.pl vc $(srcdir)/regs.dat > regvals.c
regs.h: regs.dat regs.pl
	$(PERL) $(srcdir)/regs.pl h $(srcdir)/regs.dat > regs.h

# Assembler token hash
tokhash.c: insns.dat regs.dat tokens.dat tokhash.pl perllib/phash.ph
	$(PERL) $(srcdir)/tokhash.pl c $(srcdir)/insns.dat $(srcdir)/regs.dat \
		$(srcdir)/tokens.dat > tokhash.c

# Assembler token metadata
tokens.h: insns.dat regs.dat tokens.dat tokhash.pl perllib/phash.ph
	$(PERL) $(srcdir)/tokhash.pl h $(srcdir)/insns.dat $(srcdir)/regs.dat \
		$(srcdir)/tokens.dat > tokens.h

# Preprocessor token hash
pptok.h: pptok.dat pptok.pl perllib/phash.ph
	$(PERL) $(srcdir)/pptok.pl h $(srcdir)/pptok.dat pptok.h
pptok.c: pptok.dat pptok.pl perllib/phash.ph
	$(PERL) $(srcdir)/pptok.pl c $(srcdir)/pptok.dat pptok.c

# This target generates all files that require perl.
# This allows easier generation of distribution (see dist target).
PERLREQ = macros.c insnsb.c insnsa.c insnsd.c insnsi.h insnsn.c \
	  regs.c regs.h regflags.c regdis.c regvals.c tokhash.c tokens.h \
	  version.h version.mac pptok.h pptok.c
perlreq: $(PERLREQ)

clean:
	-del /f *.obj
	-del /f *.il*
	-del /f *.tds
	-del /f *.idb
	-del /f *.pdb
	-del /f *.s
	-del /f *.i
	-del /f lib\*.obj
	-del /f lib\*.s
	-del /f lib\*.i
	-del /f output\*.obj
	-del /f output\*.s
	-del /f output\*.i
	-del /f nasm.exe
	-del /f ndisasm.exe
	rem cd rdoff && $(MAKE) clean


